﻿@using Microsoft.AspNetCore.Http.Features
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Authentication;
@using Microsoft.AspNetCore.Authentication.Cookies;
@using Microsoft.AspNetCore.Authorization;

@inject IHttpContextAccessor hca
@inject IJSRuntime JSRuntime


@if (showBanner)
{
<div id="cookieConsent" class="alert alert-info alert-dismissible fade show" role="alert">
    NetLearner uses cookies and requires your consent.
    <button type="button" class="accept-policy close" data-dismiss="alert" aria-label="Close" data-cookie-string="@cookieString" @onclick="AcceptMessage">
        <span aria-hidden="true">Accept</span>
    </button>
</div>

    @code{ 
        ITrackingConsentFeature consentFeature;
        bool showBanner;
        string cookieString;

        protected override void OnInitialized()
        {
            consentFeature = hca.HttpContext.Features.Get<ITrackingConsentFeature>();
            showBanner = !consentFeature?.CanTrack ?? false;
            cookieString = consentFeature?.CreateConsentCookie();
            Console.WriteLine(cookieString);
        }

        private void AcceptMessage()
        {
            JSRuntime.InvokeVoidAsync(
                "gatewayJsFunctions.acceptMessage",
                cookieString);
        }
    }
}
